package com.yogee.topseller.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.bean.CenterInfoBean;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/3.
 */

public class ButlerServiceLogisticsRvAdapter extends RecyclerView.Adapter<ButlerServiceLogisticsRvAdapter.ItemHolder> {

    private Context context;
    private HashMap<Integer, Boolean> isOpenHas;
    private List<CenterInfoBean.OrderDisBean> data;

    public ButlerServiceLogisticsRvAdapter(Context context) {
        this.context = context;
        isOpenHas = new HashMap<>();
    }

    public void addData(List<CenterInfoBean.OrderDisBean> data) {
        this.data = data;
        for (int i = 0; i < data.size(); i++) {
            isOpenHas.put(i, false);
        }
        notifyDataSetChanged();
    }

    public void notiF() {
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_butler_service_logistics, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {

        CenterInfoBean.OrderDisBean bean = data.get(position);
        holder.nameTv.setText(bean.getLastDisMessage());
        holder.timeTv.setText(bean.getLastTime());

        holder.logisticsRv.setLayoutManager(new LinearLayoutManager(context));
        holder.logisticsRv.setHasFixedSize(true);

        ItemRvAdapter itemRvAdapter = new ItemRvAdapter(position, bean.getDisMessage());
        holder.logisticsRv.setAdapter(itemRvAdapter);
//        holder.upV.setVisibility(View.GONE);
        /*
        if (isOpenHas != null) {
            if (!isOpenHas.get(position)) {
                holder.clickIv.setVisibility(View.VISIBLE);
                holder.logisticsLl.setVisibility(View.VISIBLE);
                holder.logisticsRv.setVisibility(View.GONE);
            } else {
                holder.logisticsRv.setVisibility(View.VISIBLE);
                holder.logisticsLl.setVisibility(View.GONE);
                holder.clickIv.setVisibility(View.GONE);
            }
        }
        holder.clickRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isOpenHas.get(position)) {
                    isOpenHas.put(position, false);
                    notifyDataSetChanged();
                } else {
                    isOpenHas.put(position, true);
                    notifyDataSetChanged();
                }
            }
        });
        */
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_butler_service_logistics_title_tv)
        TextView itemButlerServiceLogisticsTitleTv;
        @BindView(R.id.item_butler_service_logistics_rv)
        RecyclerView logisticsRv;
        @BindView(R.id.item_butler_service_logistics_cir)
        ImageView itemButlerServiceLogisticsCir;
        @BindView(R.id.item_butler_service_logistics_up_v)
        View upV;
        @BindView(R.id.item_butler_service_logistics_down_v)
        View itemButlerServiceLogisticsDownV;
        @BindView(R.id.item_butler_service_logistics_line_v)
        View itemButlerServiceLogisticsLineV;
        @BindView(R.id.item_butler_service_logistics_name_tv)
        TextView nameTv;
        @BindView(R.id.item_butler_service_logistics_time_tv)
        TextView timeTv;
        @BindView(R.id.item_butler_service_logistics_ll)
        LinearLayout logisticsLl;
        @BindView(R.id.item_butler_service_logistics_click_iv)
        ImageView clickIv;
        @BindView(R.id.item_butler_service_logistics_click_rl)
        RelativeLayout clickRl;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ItemRvAdapter extends RecyclerView.Adapter<ItemRvAdapter.ItemHolder> {
        private int pos;
        private List<CenterInfoBean.OrderDisBean.DisMessageBean> datas;

        public ItemRvAdapter(int pos, List<CenterInfoBean.OrderDisBean.DisMessageBean> data) {
            this.pos = pos;
            this.datas = data;
        }

        @Override
        public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_logistics, parent, false);
            return new ItemHolder(view);
        }

        @Override
        public void onBindViewHolder(ItemHolder holder, int position) {
            CenterInfoBean.OrderDisBean.DisMessageBean bean = datas.get(position);
            holder.nameTv.setText(bean.getCenter());
            holder.timeTv.setText(bean.getTime());

            if (position == 0) {
                holder.upV.setVisibility(View.GONE);
            } else {
                holder.nameTv.setTextColor(Color.parseColor("#9b9b9b"));
                holder.timeTv.setTextColor(Color.parseColor("#9b9b9b"));
                holder.logisticsCir.setImageResource(R.drawable.corens_100_style_s_un);
            }
            holder.clickIv.setVisibility(View.GONE);
            if (position == datas.size() - 1) {
                holder.downV.setVisibility(View.GONE);
                holder.clickIv.setVisibility(View.VISIBLE);
                holder.clickRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isOpenHas.put(pos, false);
                        notiF();
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return datas == null ? 0 : datas.size();
        }

        class ItemHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.item_logistics_cir)
            ImageView logisticsCir;
            @BindView(R.id.item_logistics_up_v)
            View upV;
            @BindView(R.id.item_logistics_down_v)
            View downV;
            @BindView(R.id.item_logistics_line_v)
            View lineV;
            @BindView(R.id.item_logistics_name_tv)
            TextView nameTv;
            @BindView(R.id.item_logistics_time_tv)
            TextView timeTv;
            @BindView(R.id.item_logistics_click_iv)
            ImageView clickIv;
            @BindView(R.id.item_logistics_click_rl)
            RelativeLayout clickRl;

            public ItemHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
